package sanshui.system.trade.util;

import com.alibaba.fastjson.JSONObject;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.*;
import org.springframework.web.client.RestTemplate;

@Slf4j
public class PushPlusUtil {
    private static String url = "http://www.pushplus.plus/send";
    @Data
    public static class PushPlusModel{
        String token = "e1fa3f075d044647bffcaead91b5e622";
        String topic;
        String template;
        String title;
        String content;
    }

    public static boolean send(PushPlusModel model){
        RestTemplate restTemplate = new RestTemplate();

        // 发送JSON格式的POST请求
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);
        HttpEntity<String> request = new HttpEntity<>(JSONObject.toJSONString(model), headers);
        ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, request, String.class);
        if (responseEntity.getStatusCode().equals(HttpStatus.OK)){
            return true;
        }
        log.error("发起失败, body:{}", responseEntity.getBody());
        return false;
    }

    public static void main(String[] args) {
        PushPlusModel pushPlusModel = new PushPlusModel();
        pushPlusModel.setTitle("测试");
        pushPlusModel.setContent("测试");
        pushPlusModel.setTopic("Watery_Trade");
        pushPlusModel.setTemplate("txt");

        send(pushPlusModel);
    }
}
